package com.example.zzl.adapter.HomeFragment;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.example.zzl.R;
import com.example.zzl.adapter.RecyclerViewBaseAdapter;
import com.example.zzl.custom.MyTag;
import com.example.zzl.databinding.HomeItemivIvBinding;
import com.example.zzl.model.bean.Home.BrandExclusive.ResultBean;
import com.example.zzl.utils.DisplayUtils;
import com.example.zzl.utils.specification.VolleySingleton;

import java.util.List;

/**
 * Created by ligg on 2017/2/9.
 */

public class RecommendIvRvAdapter extends RecyclerViewBaseAdapter<ResultBean.ListRecommendInfoBean, RecommendIvRvAdapter.ViewHolder> {

    public int itemPosition;

    public RecommendIvRvAdapter(Context context, List<ResultBean.ListRecommendInfoBean> list, int itemPosition) {
        super(context, list);
        this.itemPosition = itemPosition;
    }

    @Override
    public int getItemCount() {
        if (list != null && list.size() > 0) {
            return list.size() + 1;
        } else {
            return 0;
        }

    }

    @Override
    public int getItemViewType(int position) {
        if (position != getItemCount() - 1) {
            return 0;
        } else {
            return 1;
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            HomeItemivIvBinding db = DataBindingUtil.inflate(inflater, R.layout.home_itemiv_iv, parent, false);
            ViewHolder viewHolder = new ViewHolder(db.getRoot());
            viewHolder.db = db;
            return viewHolder;
        } else {
            ImageView imageView = new ImageView(context);
            imageView.setLayoutParams(new RecyclerView.LayoutParams(DisplayUtils.dipTopx(70), DisplayUtils.dipTopx(70)));
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return new ViewHolder(imageView);
        }

    }




    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);

        if (getItemViewType(position) == 0) {
            ResultBean.ListRecommendInfoBean info = list.get(position);
            holder.db.setInfo(info);
            holder.db.executePendingBindings();
            final ImageView iv = holder.db.iv;
            iv.setImageBitmap(null);

            iv.setTag(new MyTag(info.strMainImage, itemPosition));
            VolleySingleton.volleySingleton.getImageLoader().get(info.strMainImage, new ImageLoader.ImageListener() {
                @Override
                public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                    if (((MyTag) iv.getTag()).tag.equals(response.getRequestUrl())) {
                        iv.setImageBitmap(response.getBitmap());
                    }
                }

                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });
        } else {
            ((ImageView) holder.itemView).setImageResource(R.drawable.ic_launcher);
        }
    }



    class ViewHolder extends RecyclerView.ViewHolder {

        HomeItemivIvBinding db;

        public ViewHolder(View itemView) {
            super(itemView);
            itemView.setTag("iv");
        }
    }
}
